//
// Created by hjie on 23-5-13.
//
#include "./../redis_message.h"
#include <iostream>
#include <vector>
#include <unistd.h>
#include <sys/time.h>

std::vector<std::string> ReadyMessage()
{
    std::vector<std::string> message_vec = {
            "*2\r\n$3\r\nfoo\r\n$3\r\nbar\r\n",
            "*5\r\n$4\r\nABCD\r\n:123456\r\n$10\r\nMNBVCABCDE\r\n:12345678\r\n$9\r\n1234567890\r\n",
            "$4\r\nABCD\r\n",
            "$10\r\nmnbvcxzaws\r\n",
            ":123456\r\n",
            "-Error unknown command\r\n",
            "*10\r\n:1\r\n:2\r\n:3\r\n:4\r\n$6\r\nfoobar\r\n:99887362\r\n$10\r\nqwertyuiop\r\n$10\r\nqwertyuiop\r\n$10\r\nqwertyuiop\r\n$10\r\nqwertyuiop\r\n",
            "+abcedfkfjfghjghhthhrtkekek\r\n"
    };
    return message_vec;
}

int64_t BenchTest(int times)
{
    srand(time(NULL));
    std::vector<std::string> message_vec = ReadyMessage();
    int length = 0;

    EmRedisParseStatus status;

    struct timeval begin_tm = {.tv_sec = 0, .tv_usec = 0};
    gettimeofday(&begin_tm, NULL);

    for (int index = 0; index < times; index++)
    {
        std::string & msg = message_vec.at(index % message_vec.size());
        RedisMessage * message = RedisMessage::ParseRedisMessage((const uint8_t*)msg.c_str(), msg.size(), length, status);
        if (message == nullptr && length != (int)msg.size())
        {
            std::cout << "parse msg :" << msg << " Error" << std::endl;
            break;
        }
        else
        {
            delete message;
        }
    }
    struct timeval end_tm = {.tv_sec = 0, .tv_usec = 0};
    gettimeofday(&end_tm, NULL);

    return (end_tm.tv_sec * 1000 + end_tm.tv_usec / 1000) - (begin_tm.tv_sec * 1000 + begin_tm.tv_usec / 1000);
}

int main(int argc, const char * argv[])
{
    int times = 10000;
    int64_t spend_time = BenchTest(times);
    std::cout << times << " time : " << spend_time << "ms" << std::endl;

    times = 200000;
    spend_time = BenchTest(times);
    std::cout << times << " time : " << spend_time << "ms" << std::endl;
    return EXIT_SUCCESS;
}